import 'dart:io';
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter_upgrade/src/app_market.dart';
import 'package:flutter_upgrade/src/flutter_upgrade.dart';
import 'package:flutter_upgrade/src/liquid_progress_indicator.dart';

///
/// des:app升级提示控件
///
class SimpleAppUpgradeWidget extends StatefulWidget {
  const SimpleAppUpgradeWidget({@required this.title,
    this.titleStyle,
    @required this.contents,
    this.contentStyle,
    this.cancelText,
    this.cancelTextStyle,
    this.okText,
    this.okTextStyle,
    this.okBackgroundColors,
    this.progressBar,
    this.progressBarColor,
    this.borderRadius = 10,
    this.downloadUrl,
    this.force = false,
    this.iosAppId,
    this.appMarketInfo});

  ///
  /// 升级标题
  ///
  final String title;

  ///
  /// 标题样式
  ///
  final TextStyle titleStyle;

  ///
  /// 升级提示内容
  ///
  final List<String> contents;

  ///
  /// 提示内容样式
  ///
  final TextStyle contentStyle;

  ///
  /// 下载进度条
  ///
  final Widget progressBar;

  ///
  /// 进度条颜色
  ///
  final Color progressBarColor;

  ///
  /// 确认控件
  ///
  final String okText;

  ///
  /// 确认控件样式
  ///
  final TextStyle okTextStyle;

  ///
  /// 确认控件背景颜色,2种颜色左到右线性渐变
  ///
  final List<Color> okBackgroundColors;

  ///
  /// 取消控件
  ///
  final String cancelText;

  ///
  /// 取消控件样式
  ///
  final TextStyle cancelTextStyle;

  ///
  /// app安装包下载url,没有下载跳转到应用宝等渠道更新
  ///
  final String downloadUrl;

  ///
  /// 圆角半径
  ///
  final double borderRadius;

  ///
  /// 是否强制升级,设置true没有取消按钮
  ///
  final bool force;

  ///
  /// ios app id,用于跳转app store
  ///
  final String iosAppId;

  ///
  /// 指定跳转的应用市场，
  /// 如果不指定将会弹出提示框，让用户选择哪一个应用市场。
  ///
  final AppMarketInfo appMarketInfo;

  @override
  State<StatefulWidget> createState() => _SimpleAppUpgradeWidget();
}

class _SimpleAppUpgradeWidget extends State<SimpleAppUpgradeWidget> {
  static final String _downloadApkName = 'temp.apk';

  ///
  /// 下载进度
  ///
  double _downloadProgress = 0.0;

  ///
  /// 是否下载
  ///
  bool _isDownload = false;

  @override
  Widget build(BuildContext context) {
    return Container(
      child: _buildInfoWidget(context),
    );
  }

  ///
  /// 信息展示widget
  ///
  Widget _buildInfoWidget(BuildContext context) {
    return WillPopScope(
      onWillPop: () => Future.value(false),
      child: Container(
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: <Widget>[
            //标题
            _buildTitle(),
            Stack(
              children: <Widget>[
                Divider(
                  height: 0.5,
                  color: Colors.black12,
                ),
                Container(
                  height: 2.0,
                  child: _downloadProgress>0 ? new LinearProgressIndicator(
                      value: _downloadProgress,
                      valueColor: new AlwaysStoppedAnimation<Color>(
                          Colors.red)):Container(),
                )
              ],
            ),
            //更新信息
            _buildAppInfo(),
            //操作按钮
            _buildAction()
          ],
        ),
      ),
    );
  }

  ///
  /// 构建标题
  ///
  _buildTitle() {
    return Padding(
        padding: EdgeInsets.only(top: 22, bottom: 15),
        child: Text(widget.title ?? '',
            style: widget.titleStyle ?? TextStyle(fontSize: 15)));
  }

  ///
  /// 构建版本更新信息
  ///
  _buildAppInfo() {
    return Container(
        padding: EdgeInsets.only(top: 22, left: 23, right: 23),
        height: 200,
        child: ListView(
          physics: const NeverScrollableScrollPhysics(),
          children: widget.contents.map((f) {
            return Text(
              f,
              style: widget.contentStyle ?? TextStyle(),
            );
          }).toList(),
        ));
  }

  ///
  /// 构建取消或者升级按钮
  ///
  _buildAction() {
    return Column(
      children: <Widget>[
        Divider(
          height: 0.5,
          color: Colors.black12,
        ),
        Row(
          children: <Widget>[
            Visibility(
              visible: !_isDownload&& !widget.force,
              child: Expanded(
                child: _buildCancelActionButton(),
              ),
            ),
            Visibility(
              visible: !_isDownload&& !widget.force,
              child: Container(
                width: 0.5,
                height: 48,
                color: Colors.black12,
              ),
            ),
            Visibility(
              visible: !_isDownload,
              child: Expanded(
                child: _buildOkActionButton(),
              ),
            ),
            Visibility(
              visible: _isDownload,
              child: Expanded(
                child: Center(
                  child: Container(
                    alignment: Alignment.center,
                    height: 41.5,
                    child: Text(
                      "下载完喊我哦~",
                      style: TextStyle(
                          fontSize: 15,
                          color: Colors.red),
                    ),
                  ),
                ),
              ),
            )
          ],
        ),
      ],
    );
  }

  ///
  /// 取消按钮
  ///
  _buildCancelActionButton() {
    return Ink(
      decoration: BoxDecoration(
          borderRadius: BorderRadius.only(
              bottomLeft: Radius.circular(widget.borderRadius))),
      child: InkWell(
        borderRadius:
        BorderRadius.only(bottomLeft: Radius.circular(widget.borderRadius)),
        child: Container(
          height: 45,
          alignment: Alignment.center,
          child: Text(widget.cancelText ?? '以后再说',
              style: widget.cancelTextStyle ?? TextStyle()),
        ),
        onTap: () => Navigator.of(context).pop(),
      ),
    );
  }

  ///
  /// 确定按钮
  ///
  _buildOkActionButton() {
    var borderRadius =
    BorderRadius.only(bottomRight: Radius.circular(widget.borderRadius));
    if (widget.force) {
      borderRadius = BorderRadius.only(
          bottomRight: Radius.circular(widget.borderRadius),
          bottomLeft: Radius.circular(widget.borderRadius));
    }
    var _okBackgroundColors = widget.okBackgroundColors;
    if (widget.okBackgroundColors == null ||
        widget.okBackgroundColors.length != 2) {
      _okBackgroundColors = [
        Theme
            .of(context)
            .primaryColor,
        Theme
            .of(context)
            .primaryColor
      ];
    }
    return Ink(
      decoration: BoxDecoration(
          gradient: LinearGradient(
              begin: Alignment.topLeft,
              end: Alignment.bottomRight,
              colors: [_okBackgroundColors[0], _okBackgroundColors[1]]),
          borderRadius: borderRadius),
      child: InkWell(
        borderRadius: borderRadius,
        child: Container(
          height: 45,
          alignment: Alignment.center,
          child: Text(widget.okText ?? '立即体验',
              style: widget.okTextStyle ?? TextStyle()),
        ),
        onTap: () {
          _clickOk();
        },
      ),
    );
  }

  ///
  /// 下载进度widget
  ///
  Widget _buildDownloadProgress() {
    return widget.progressBar ??
        LiquidLinearProgressIndicator(
          value: _downloadProgress,
          direction: Axis.vertical,
          valueColor: AlwaysStoppedAnimation(widget.progressBarColor ??
              Theme
                  .of(context)
                  .primaryColor
                  .withOpacity(0.4)),
          borderRadius: widget.borderRadius,
        );
  }

  ///
  /// 点击确定按钮
  ///
  _clickOk() async {
    if (Platform.isIOS) {
      Navigator.of(context).pop();
      //ios 需要跳转到app store更新，原生实现
      FlutterUpgrade.toAppStore(widget.iosAppId);
      return;
    }
    if (widget.downloadUrl == null || widget.downloadUrl.isEmpty) {
      //没有下载地址，跳转到第三方渠道更新，原生实现
      FlutterUpgrade.toMarket(appMarketInfo: widget.appMarketInfo);
      return;
    }
    _isDownload = true;
    String path = await FlutterUpgrade.apkDownloadPath;
    _downloadApk(widget.downloadUrl, '$path/$_downloadApkName');
  }

  ///
  /// 下载apk包
  ///
  _downloadApk(String url, String path) async {
    try {
      var dio = Dio();
      await dio.download(url, path, onReceiveProgress: (int count, int total) {
        if (total == -1) {
          _downloadProgress = 0.01;
        } else {
          _downloadProgress = count / total.toDouble();
        }
        setState(() {});
        if (_downloadProgress == 1) {
          //下载完成，跳转到程序安装界面
          FlutterUpgrade.installAppForAndroid(path);
        }
      });
    } catch (e) {
      print('$e');
    }
  }
}
